package cn.itcast.controller;

import cn.itcast.domain.Dept;
import cn.itcast.service.DeptService;
import cn.itcast.service.impl.DeptServiceImpl;
import cn.itcast.utils.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/dept/*")
public class DeptServlet extends BaseServlet {
    private DeptService deptService = new DeptServiceImpl();

    // TODO: 2020年7月23日01:09:07   ********  查询所有功能  *********
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String page = req.getParameter("page");
        int pageNum = 1;
        if (StringUtils.isNoneBlank(page)) {
            pageNum = Integer.parseInt(page);
        }
        //调用service方法查询所有
        List<Dept> deptList = deptService.findAll(pageNum);
        //分页
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);

        //存储到域中
        req.setAttribute("page", pageInfo);

        //跳转到.list.jsp页面
        req.getRequestDispatcher("/pages/dept/list.jsp").forward(req, resp);

    }

    // TODO: 2020年7月23日02:10:56   ********  跳转新增页面功能  *********
    protected void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Dept> deptList = deptService.findAll();
        req.setAttribute("deptList", deptList);
        req.getRequestDispatcher("/pages/dept/add.jsp").forward(req, resp);
    }

    // TODO: 2020年7月23日02:12:16   ********  新增功能  *********
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        //调用service添加
        deptService.add(dept);

        //重新查询数据库,遍历
        resp.sendRedirect(req.getContextPath() + "/store/dept/findAll");
    }

    // TODO: 2020年7月23日03:36:24   ********  数据回显功能  *********
    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        Dept dept = deptService.findById(id);
        //System.out.println(dept);
        List<Dept> deptList = deptService.findAll();
        req.setAttribute("dept", dept);
        req.setAttribute("deptList", deptList);

        req.getRequestDispatcher("/pages/dept/update.jsp").forward(req,resp);

    }

    // TODO: 2020年7月23日04:13:40   ********  修改功能  *********
    protected void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dept dept = BeanUtil.fillBean(req, Dept.class);
        deptService.update(dept);
        resp.sendRedirect(req.getContextPath() + "/store/dept/findAll");
    }

    // TODO: 2020年7月23日04:30:00   ********  批量删除功能  *********
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端数据
        String ids = req.getParameter("ids");
        //调用service方法删除
        deptService.deleteByIds(ids);

        //重新访问数据库,并遍历
        resp.sendRedirect(req.getContextPath()+"/store/dept/findAll");
    }

}
